package com.tjp.eight.comment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;

import com.tjp.eight.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * Created by Administrator on 2017/2/13.
 */

public class AppManager {
    private static Stack<Activity> activityStack;
    private static AppManager appManager;

    private AppManager() {
    }

    public static AppManager getAppManager() {
        if (appManager == null) {
            appManager = new AppManager();
        }
        return appManager;
    }

    public void AddActivity(Activity activity) {
        if (activityStack == null) {
            activityStack = new Stack<>();
        }
        activityStack.add(activity);
    }

    Activity currentActivity() {
        return activityStack.lastElement();
    }

    public Activity findActivityByClassName(Class<?> clz) {
        Activity acty = null;
        int stackSize = -1;

        if (clz == null) {
            return acty;
        }

        if (activityStack == null) {
            return acty;
        }

        stackSize = activityStack.size();
        for (int i = 0; i < stackSize; i++) {
            if (clz.getCanonicalName().equals(activityStack.get(i).getClass().getCanonicalName()) && !activityStack.get(i).isFinishing()) {
                acty = activityStack.get(i);
                break;
            }
        }

        if (acty == null || acty.isFinishing()) {
            acty = null;
        }

        return acty;
    }

    /**
     * 结束当前Activity（堆栈中最后一个压入的）
     */
    public void finishActivity() {
        Activity activity = activityStack.lastElement();
        finishActivity(activity);
    }


    /**
     * 结束指定的Activity
     */
    public synchronized void finishActivity(Activity activity) {
        if (activity != null) {
            activityStack.remove(activity);
            activity.finish();
            activity = null;
        }
    }


    public void notFinishActivity(Class<?> cls) {
        List<Activity> actys = new ArrayList<Activity>();
        for (Activity activity : activityStack) {
            if (!activity.getClass().equals(cls)) {
                actys.add(activity);
            }
        }
        for (Activity activity : actys) {
            finishActivity(activity);
        }
    }


    /**
     * 结束所有Activity
     */
    private synchronized void finishAllActivity() {
        for (int i = 0, size = activityStack.size(); i < size; i++) {
            if (null != activityStack.get(i)) {
                activityStack.get(i).finish();
                activityStack.get(i).overridePendingTransition(R.anim.umeng_socialize_fade_in, R.anim.umeng_socialize_fade_out);
            }
        }
        activityStack.clear();
    }

    /**
     * 退出应用程序
     */
    @SuppressLint("NewApi")
    public void AppExit(Context context) {
        try {
            finishAllActivity();
            ActivityManager activityMgr = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            activityMgr.killBackgroundProcesses(context.getPackageName());
            System.exit(0);
        } catch (Exception e) {
        }
    }

    public void Exit() {
        finishAllActivity();
    }
}
